package com.boolib.http.sign.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.digest.MD5;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

/**
 * @author wfq
 */
public class SignUtil {


    static  String privateKey  = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDqn1f6UPQ8Lf5s" +
            "g6B8CGOGg/odjf/MRxOzysZgLO2QtP2h9vqa9Tu+L+op9gS2G37pVbKIfftJxzzM" +
            "skZGETN0qTScW9qyhigEkoZoBOWqsMObXa7cyemGEy0sBE6zMrKoU36AqbPsQSeN" +
            "WY7DyrrHWgdBOa+yWh2ElKukPYdEK4yykRSdhxTbvdOfSTa6WiLzOlNwPv+qwwRO" +
            "PlEAsR0KtqNGVXf0Unku1pElBhaYur2jWQlT7zHOCmzD/O8m/+iEcPn71PAZi3zb" +
            "D7NjUArdiVhMjOgb1I4OVdd8kCrDEHNwdMJfvQ02UklXVrVqNZkKFjpHcOUzLyhq" +
            "dsM3hPc9AgMBAAECggEBAKkxwcMc8BQZ4vkkLu5jKkqOECeSCF6q/xVP7KDcpXEO" +
            "ZD2RmwV2ReAQM6CQ7rLYvzvLdhUiLL32tOy0vyK2MtI1V2BOiTbmsiMh+1U1+t2N" +
            "3XHELF7CKOpsF84/SPXHYLkshPSJ7Ns53Sj85YxiPTGXxdWOoRpQDCtY/zJQwKQy" +
            "FhV6DVsxG+Dq8GVW0tC+MwFNeayKmOXZebkali6mMIKu7o3NW/t/DlALBkxbHTzM" +
            "u6u6PoapRj+zizmyFwmP6ls8mYkBges1qyxuO1eR6va2luIC4o5P2dJ1FJNj55lU" +
            "oMIq6e1UiUCHse7YxdYQyEsZY8Ed+i2k4t+kqFbC2MECgYEA+l+KZWmNY5uftkDH" +
            "eAqedPiA90TqThvpxSa2wcR6fjMDUvWZfwqf3KEPgRqcydsUZNx6TkxkD/PWrpI4" +
            "B8zmOemid3OhpszQiHuyuPh2wluW8jBA6c8byF7EhvNc34W5LFsa7hTaWE90pINP" +
            "HRJwLo5ae1ZYMO2M5Vu+JrhPM60CgYEA7+UvWUKIcytQHslujXTHAeG9UHrY0OhY" +
            "4iVjY/WuHNYcZwNNd4CqyRACmfsjq4BkZUPrzL4gUJlOMBRvlLMx5pMvgivW54jk" +
            "BarnFvm2rEDUUmxChZed3dkhduJCdfcqfd6KcaLmRIgL1zNs3jKnAol2Ce2gNn9k" +
            "XhsUSLxbw9ECgYB3Y6Wn9CxCykvfaYZ0fIPQ/UrqHkeoVXEk3PAa9+j9UrAMGCdI" +
            "yDUwzPCKvLj1HG3eJrB/s7enT7wYZIspVRCXnwwk79OuqU3rNykjQP/IcEpLVK0v" +
            "kN4Lrv0B6Pxpbee9+feeff0cTU8uv5qzHz61RJZq5+KH+k7Mc/H5nrvNxQKBgQCR" +
            "3n/9/JDPxuR+8+i/JuTjZgohZ7FyxfVV/XoEDB2E90Rp9v4hziELBSsvyh+UZwuk" +
            "77nv0yP/HlkGr0Zx2sxa8eCPfGy+eOR99b9ynwDOmNUCEUYZyWl6vk605TuYU2SJ" +
            "CMqR8oWl4jug7WojahVXW5Jk0xL6UFgNSNX6rF34IQKBgQDSkMvWgfhmMdYYnayE" +
            "uVlzRba+ZtjgAaEffc1VSPeWQxVO7nQnoJF1jHsFAfabS6QeHvT9O4J9ItTIBJe0" +
            "MqFngloyZjLQ/RfXCLzqsQVcm8RYjzNrlFv9/5BNZSrOC0oR0akFLw4ymb3RzjNP" +
            "fN3d2Au3+3O5DzOaMbnMMv/GFg==";
    static String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6p9X+lD0PC3+bIOgfAhj" +
            "hoP6HY3/zEcTs8rGYCztkLT9ofb6mvU7vi/qKfYEtht+6VWyiH37Scc8zLJGRhEz" +
            "dKk0nFvasoYoBJKGaATlqrDDm12u3MnphhMtLAROszKyqFN+gKmz7EEnjVmOw8q6" +
            "x1oHQTmvslodhJSrpD2HRCuMspEUnYcU273Tn0k2uloi8zpTcD7/qsMETj5RALEd" +
            "CrajRlV39FJ5LtaRJQYWmLq9o1kJU+8xzgpsw/zvJv/ohHD5+9TwGYt82w+zY1AK" +
            "3YlYTIzoG9SODlXXfJAqwxBzcHTCX70NNlJJV1a1ajWZChY6R3DlMy8oanbDN4T3" +
            "PQIDAQAB";
    


    final static RSA rsa = new RSA(privateKey,publicKey);

    static  String salt = "md5:44459ffd-d58d-445b-a1d1-754f2c5f34d4";


    /**
     * 签名
     * @param msg
     * @return
     */
    public static String sign (String msg){
        String message = new StringBuilder().append(msg).append(salt).toString();
        return   SecureUtil.md5(message);
    }


    /**
     * 验证
     * @param data
     * @param signStr
     * @return
     */
    public static boolean verifySign(String data,String signStr){
        String message = new StringBuilder().append(data).append(salt).toString();

        String s = MD5.create().digestHex(message);

        return signStr.equalsIgnoreCase(s)  ;
    }


    /**
     * 加密
     */
    public static byte[] encode(byte[] data) throws UnsupportedEncodingException {
        return  rsa.encrypt(data, KeyType.PublicKey);
    }

    public static byte[] decode(byte[] data){

      return  rsa.decrypt(data, KeyType.PrivateKey);

    }


}
